package cn.jobstart.common.spider;

import cn.jobstart.common.StringUtil;
import cn.jobstart.common.redis.RedisUtil;
import cn.jobstart.props.FileConfig;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2023-10-11 09:53
 */

@Component
public class Job51Util extends TokenUtil{


    private static final String JOB51_TOKEN = "job51_token";
    //  private String authorization;
    private static final String JOB51_COOKIE = "job51_cookie";


    public void Job51Util() {

    }



    public String getAuthorization() {


        //得到token
        try {
            return (String) getRedisUtil().get(JOB51_TOKEN);
        } catch (Exception e) {

        }

        return null;
    }

    public Job51Util setCooke(Set<Cookie> cookies, long expireTime) {
        if (StringUtil.isNullOrEmpty(cookies)) {
            return this;
        }
        String cookieStr = "";

        cookieStr = cookies.stream().map(item -> item.getName() + "=" + item.getValue()).collect(Collectors.joining(";"));
        getRedisUtil().set(JOB51_COOKIE, cookieStr, expireTime);

        return this;


    }

    public String getCookie() {
        try {
            return (String) getRedisUtil().get(JOB51_COOKIE);
        } catch (Exception e) {
            return null;
        }

    }

    public Job51Util setAuthorization(String authorization, long expireTime) {


        getRedisUtil().set(JOB51_TOKEN, authorization, expireTime);

        return this;
    }

    public void removeToken() {
        try {
            getRedisUtil().del(JOB51_TOKEN);
        } catch (Exception e) {

        }
        try {
            getRedisUtil().del(JOB51_COOKIE);


        } catch (Exception e) {

        }


    }

    public void initAuthorization() {

        System.getProperties().setProperty("webdriver.chrome.driver", getFileConfig().getChromeDriverPath());
        System.setProperty("webdriver.chrome.whitelistedIps", "");
        do {
            if (StringUtil.isNotObjEmpty(getAuthorization())) {
                return;
            }

            createAuthorization();
        } while (true);
    }


    public void createAuthorization() {

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--allowed-ips", "--disable-web-security");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.manage().window().maximize();
        // 添加一个隐式等待，用于等待页面元素加载完成
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // webDriver.get("https://ehire.51job.com/Revision/login?returl=Revision%2Fnavigate%3Frt%3D1696774493237");
        webDriver.get("https://ehire.51job.com/Revision/login");
        sleep(2000);

        // webDriver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div[3]/div/div/div/div[2]/span/span")).click();
        //sleep(1000);


        WebElement webElement = webDriver.findElement(By.xpath("//*[@id=\"sensor_home_passwordlogin\"]"));
        webElement.click();
        sleep(1000);
        //  webDriver.findElement(By.xpath("//*[@id=\"pane-password\"]/div/div[1]/div/input")).sendKeys("277195123");
        webDriver.findElement(By.xpath("//*[@id=\"pane-password\"]/div/div[1]/div/input")).sendKeys("77312503");
        sleep(1000);
        // webDriver.findElement(By.xpath("//*[@id=\"pane-password\"]/div/div[2]/div/input")).sendKeys("19297816");
        webDriver.findElement(By.xpath("//*[@id=\"pane-password\"]/div/div[2]/div/input")).sendKeys("19436145");
        sleep(1000);
        webDriver.findElement(By.xpath("//*[@id=\"pane-password\"]/div/div[3]/div/input")).sendKeys("blue@123");
        sleep(2000);
        webDriver.findElement(By.xpath("//*[@id=\"sensor_home_login_01\"]")).click();
        sleep(5000);


        Set<Cookie> cookies = webDriver.manage().getCookies();
        long expireTime = 6 * 24 * 3600 + 12 * 3600; //有效期设置成6天半
        for (Cookie cookie : cookies) {
            if ("authorization".equalsIgnoreCase(cookie.getName())) {
                setAuthorization(cookie.getValue(), expireTime);
                break;
            }
        }
        setCooke(cookies, expireTime);

        webDriver.close();

    }


}
